<?php
/*
*	Kleophatra - Open Source Content Management System
*	------------------------------------------------------------------------
*	Copyright (C) 2007-2009, Sergio Milardovich <smilardovich@yahoo.com.ar>
*	This program is Free Software.
*
*	@package	Kleophatra
*	@subpackage	/lib
*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
*	@author		Sergio Milardovich <smilardovich@yahoo.com.ar>
*	@link		http://www.kleophatra.org
*/

require_once KLEO_LIB_DIR.'xmlparser.lib.php';
class kleopaginator extends kleoxmlparser{
	
	var $pagination_current;
	var $paginator_current_link;
	var $paginator_current_number;
	var $paginator_current = "<span class=\"paginator-current\">";
	var $paginator_first = "<span class=\"paginator-field\">";
	var $paginator_last = "</span>";
	var $totals;
	var $pages;
	var $limit;
	var $start;
	var $numbers = 20;
	
	function set_pagination_values($limit, $query, $url, &$q){

		if(empty($url)){
		   $url = $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
		}
		$start = 0;
		if(isset($_GET['start'])){
			$start = $_GET['start'];
		}
		
		/* for security */
		if(!is_numeric($start)){
			$this->apperror("$start must be numeric", "common");
		}

		$query = $query." LIMIT $start,$limit";

		$q = $this->query($query);
		$_SESSION['paginator_query'] = $q;

		$totals = $this->num_rows($q);
		$this->pages = round($totals/$limit+1);
		
		$this->query = $query;
		$this->limit = $limit;
		$this->start = $start;
		$this->totals = $totals;

	}

	function generate_pagination_box(&$buff, $query, $file='pagination.tpl'){
	
		$totals = $this->num_rows($this->query($query));

		if(isset($_GET['start'])){
			$first = 'href="'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'&start=0" ';
		}

		$this->tpl_load($file, $pbuff, false, 'core');

		if(!$this->start <= 0){
			if($this->start % 2 == 0){
				$back = $this->start-$this->limit;
				$back = "&start=".$back;
				$this->tpl_set("PAGINATION_BACK_URL", 'href="'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].$back.'" ', $pbuff, $pbuff);
			} else {
				$this->tpl_set("PAGINATION_BACK_URL", NULL, $pbuff, $pbuff);
			}
			if($this->start == 0){
				$this->tpl_set("PAGINATION_BACK_URL", $first, $pbuff, $pbuff);
			}

		}
/*
		for($i=1;$i<$totals;$i+$this->limit-1){
				$this->pagination_current_link = "{$this->url}?start={$i}";
				if($this->start == $i){
					$return .= '<a class="pagination_link">'.$this->paginator_current.$i.$this->paginator_last.'</a>';
				} else {
					$return .= '<a class="pagination_link" href="'.$this->pagination_current_link.'">'.$this->paginator_first.$i.$this->paginator_last.'</a>';
				}
				if($i>=$this->numbers){
					break;
				}
		
		}
*/
		if($this->start+$this->limit <= $this->totals){
			$next = $this->start+$this->limit;
			$next = "&start=".$next;
			$this->tpl_set("PAGINATION_NEXT_URL", 'href="'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].$next.'" ', $pbuff, $pbuff);
		} else {
			$this->tpl_set("PAGINATION_NEXT_URL", null, $pbuff, $pbuff);
		}

		if($this->totals >= $this->limit){
			$last = "?start=".$this->pages;
			$this->tpl_set("PAGINATION_LAST_URL", 'href="'.$_SERVER['PHP_SELF.'].'?'.$_SERVER['QUERY_STRING'].'&'.$last.'" ', $pbuff, $pbuff);
		} else {
			$this->tpl_set("PAGINATION_LAST_URL", null, $pbuff, $pbuff);
		}
		if(!isset($first)){
			$first = null;
		}
		$this->tpl_set("PAGINATION_FIRST_URL", $first, $pbuff, $pbuff);
	
		$this->tpl_set("PAGINATION_NUMBERS", null, $pbuff, $pbuff);
		$this->compile($pbuff, $buff);
		$buff = $pbuff;

	}

}
?>
